package com.xio.cloud.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xio.cloud.mapper.AccountMapper;
import com.xio.cloud.domain.Account;
import com.xio.cloud.service.AccountService;
/**
 * @Author: xio
 * @Description: ${Description}
 * @Date: 2021/3/31 20:55
 */
@Slf4j
@Service
public class AccountServiceImpl
        extends ServiceImpl<AccountMapper, Account>
        implements AccountService{

    @Override
    public boolean decrease(Long userId, BigDecimal money) {
        log.info("- - - - - > seata-account-service: 减少账户余额开始");

        // 模拟超时
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 查找账户
        Account account = lambdaQuery().eq(Account::getUserId, userId).one();

        // 减少余额
        account.setUsed(account.getUsed().add(money));
        account.setResidue(account.getResidue().subtract(money));
        boolean result = updateById(account);
        log.info("- - - - - > seata-account-service: 减少账户余额结束");
        return result;

    }
}
